package com.hexacta.hrs.web.struts.actions.feedbacks;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.vectrics.employee.Employee;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.recruiter.job.JobFunction;
import org.vectrics.user.Person;

import com.hexacta.hrs.domain.feedback.Feedback;
import com.hexacta.hrs.domain.feedback.FeedbackPeriodResumeByRole;
import com.hexacta.hrs.domain.feedback.Period;
import com.hexacta.hrs.domain.feedback.ScoreType;
import com.hexacta.hrs.domain.feedback.effort.EffortScoreType;
import com.hexacta.hrs.domain.staffing.Project;
import com.hexacta.hrs.securitygroups.PermissionName;
import com.hexacta.hrs.service.staffing.ProjectService;
import com.hexacta.hrs.services.employee.EmployeeService;
import com.hexacta.hrs.services.feedbacks.FeedbackPeriodService;
import com.hexacta.hrs.services.feedbacks.FeedbackService;
import com.hexacta.hrs.services.feedbacks.PeriodService;
import com.hexacta.hrs.services.mentoring.MentoringService;
import com.hexacta.hrs.web.struts.actions.security.PermissionChecker;
import com.opensymphony.xwork2.ActionSupport;

public class ManageFeedbacksAction extends ActionSupport implements SessionAware, ServletRequestAware {

    private static final long serialVersionUID = -7192248251486130798L;

    private Map<String, Object> session;

    private FeedbackPeriodService feedbackPeriodService;

    private FeedbackService feedbackService;

    private ProjectService projectService;

    private MentoringService mentoringService;

    private EmployeeService employeeService;

    private PeriodService periodService;

    private PermissionChecker permissionChecker = new PermissionChecker();

    private ManageFeedbacksFilters filters;

    // Lsita de Feedbacks a ser devuelta por el filtro (Por defecto findAll)
    private List<Feedback> feedbacksList;

    private EditedSkill editedSkill;

    private EditedEffort editedEffort;

    private EditedScore editedScore;

    private List<FeedbackPeriodResumeByRole> feedbackPeriodResumesByRoles;

    private Long feedbackToDelete;

    private String periodId;

    private Period period;

    private HttpServletRequest request;

    public Map<String, Object> getSession() {
        return session;
    }

    @Override
    public void setSession(final Map<String, Object> session) {
        this.session = session;
    }

    public FeedbackPeriodService getFeedbackPeriodService() {
        return feedbackPeriodService;
    }

    public void setFeedbackPeriodService(final FeedbackPeriodService feedbackPeriodService) {
        this.feedbackPeriodService = feedbackPeriodService;
    }

    public ProjectService getProjectService() {
        return projectService;
    }

    public void setProjectService(final ProjectService projectService) {
        this.projectService = projectService;
    }

    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    public MentoringService getMentoringService() {
        return mentoringService;
    }

    public void setMentoringService(final MentoringService mentoringService) {
        this.mentoringService = mentoringService;
    }

    public FeedbackService getFeedbackService() {
        return feedbackService;
    }

    public void setFeedbackService(final FeedbackService feedbackService) {
        this.feedbackService = feedbackService;
    }

    public ManageFeedbacksFilters getFilters() {
        return filters;
    }

    public void setFilters(final ManageFeedbacksFilters filters) {
        this.filters = filters;
    }

    public EditedSkill getEditedSkill() {
        return editedSkill;
    }

    public void setEditedSkill(final EditedSkill editedSkill) {
        this.editedSkill = editedSkill;
    }

    public EditedEffort getEditedEffort() {
        return editedEffort;
    }

    public void setEditedEffort(final EditedEffort editedEffort) {
        this.editedEffort = editedEffort;
    }

    public EditedScore getEditedScore() {
        return editedScore;
    }

    public void setEditedScore(final EditedScore editedScore) {
        this.editedScore = editedScore;
    }

    public List<Project> getAllProjectsList() {
        return projectService.findAll();
    }

    public List<Employee> getAllEmployeesList() {
        return (List<Employee>) employeeService.getAllCurrentEmployees();
    }

    public List<JobFunction> getAllRoleList() {
        return RecruitServiceLocator.getJobProvider().findAllJobFunctions();
    }

    public List<Employee> getAllMentorsList() {
        return (List<Employee>) mentoringService.getAllActiveMentors();
    }

    public List<Employee> getAllEvaluatorsList() {
        return (List<Employee>) feedbackService.findAllEvaluators();
    }

    public List<Feedback> getFeedbacksList() {
        return feedbacksList;
    }

    public void setFeedbacksList(final List<Feedback> feedbacksList) {
        this.feedbacksList = feedbacksList;
    }

    public List<EffortScoreType> getEffortScoreTypes() {
        return Arrays.asList(EffortScoreType.values());
    }

    public List<ScoreType> getScoreTypes() {
        return Arrays.asList(ScoreType.values());
    }

    public List<FeedbackPeriodResumeByRole> getFeedbackPeriodResumesByRoles() {
        return feedbackPeriodResumesByRoles;
    }

    public void setFeedbackPeriodResumesByRoles(final List<FeedbackPeriodResumeByRole> feedbackPeriodResumesByRoles) {
        this.feedbackPeriodResumesByRoles = feedbackPeriodResumesByRoles;
    }

    public Long getFeedbackToDelete() {
        return feedbackToDelete;
    }

    public void setFeedbackToDelete(final Long feedbackToDelete) {
        this.feedbackToDelete = feedbackToDelete;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    @Override
    public String execute() throws Exception {
        return initFilters();
    }

    public String initFilters() {

        if (StringUtils.isEmpty(periodId)) {
            try {
                period = periodService.getOpenPeriod();
                periodId = period.getYear();
            } catch (Exception e) {
                period = periodService.getLastPeriod();
                periodId = period.getYear();
            }
        } else {
            period = periodService.getPeriodByYear(periodId);
        }
        return SUCCESS;
    }

    public String filter() {

        Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(getSession());

        if (filters == null) {
            feedbacksList = new ArrayList<Feedback>();
            return SUCCESS;
        }

        period = periodService.getPeriodByYear(periodId);

        feedbacksList = new ArrayList<Feedback>(feedbackService.findFeedbacksWithCriteria(period, person.getId(),
                filters.getSelectedEmployeeId(), filters.getSelectedProjectId(), filters.getSelectedRoleId(),
                filters.getSelectedMentorId(), filters.getSelectedEvaluatorId()));

        feedbackPeriodResumesByRoles = feedbackPeriodService.getAllFeedbackPeriodResumesByRole(getPeriodId());

        return SUCCESS;
    }

    public String editFeedbackSkill() {
        feedbackService.setFeedbackSkill(editedSkill.getFeedbackId(), editedSkill.getSkillType(),
                editedSkill.getCommentPros().trim(), editedSkill.getCommentCons().trim());
        return filter();
    }

    public String editFeedbackEffort() {
        feedbackService.setFeedbackEffort(editedEffort.getFeedbackId(), editedEffort.getEffortScore(),
                editedEffort.getJustification().trim());
        return filter();
    }

    public String editFeedbackScore() {
        feedbackService.setFeedbackScore(editedScore.getFeedbackId(), editedScore.getScore());
        return filter();
    }

    public String delete() {
        feedbackService.delete(getFeedbackToDelete());
        System.out.println("Borro el " + getFeedbackToDelete());
        return filter();
    }
    
    public String getRoleForFeedbackPeriod(Feedback feedback){
    	return feedbackPeriodService.findWithFeedback(feedback).getActualBand().getDescription();
    }
    
    public Long getBandForFeedbackPeriod(Feedback feedback){
    	return feedbackPeriodService.findWithFeedback(feedback).getActualPosition();
    }
    /**
     * @return the periodId
     */
    public String getPeriodId() {
        return periodId;
    }

    /**
     * @param periodId
     *            the periodId to set
     */
    public void setPeriodId(final String periodId) {
        this.periodId = periodId;
    }

    /**
     * @return the periodService
     */
    public PeriodService getPeriodService() {
        return periodService;
    }

    /**
     * @param periodService
     *            the periodService to set
     */
    public void setPeriodService(final PeriodService periodService) {
        this.periodService = periodService;
    }

    /**
     * @return the period
     */
    public Period getPeriod() {
        return period;
    }

    /**
     * @param period
     *            the period to set
     */
    public void setPeriod(final Period period) {
        this.period = period;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
     * javax.servlet.http.HttpServletRequest)
     */
    @Override
    public void setServletRequest(final HttpServletRequest request) {
        this.request = request;
    }

    /**
     * @return the request
     */
    public HttpServletRequest getRequest() {
        return request;
    }

    /**
     * @return the showReturn
     */
    public boolean isShowReturn() {
        String permission = PermissionName.MANAGE_FEEBACKS.toString();
        return permissionChecker.hasPermission(permission, request.getSession());
    }

}